//
//  DropDownRefreshView.m
//  QQ界面
//
//  Created by apple on 16/11/30.
//  Copyright © 2016年 LWB. All rights reserved.
//

#import "DropDownRefreshView.h"
typedef enum {
    DropDownRefreshViewStautsNormal,
    DropDownRefreshViewStautsPulling,
    DropDownRefreshViewStautsRefreshing
    
}DropDownRefreshViewStauts;
@interface DropDownRefreshView()
@property(nonatomic,strong)UIImageView *imaView;
@property(nonatomic,strong)UILabel *label;
@property(nonatomic,strong)UIScrollView *superscrView;
@property(nonatomic,assign)DropDownRefreshViewStauts currentStauts;
@property(nonatomic,strong)NSArray *imagArr;
@end
@implementation DropDownRefreshView
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.imaView];
        [self addSubview:self.label];
      self.imaView.frame = CGRectMake((self.bounds.size.width-50)/2,0, 50, 50);
       self.label.frame = CGRectMake((self.bounds.size.width-50)/2+60,0, 120, 30);
        
    }
    return self;
}
-(void)dealloc{
    [_superscrView removeObserver:self forKeyPath:@"contentOffset"];
}
-(UIImageView *)imaView{
 
    if (_imaView ==nil) {
        UIImage *imag = [UIImage imageNamed:@"btn_back-1"];
        _imaView = [[UIImageView alloc]initWithImage:imag];
    }
    return _imaView;
}
-(UILabel *)label{
    if (_label ==nil) {
        _label = [[UILabel alloc]init];
        _label.font = [UIFont systemFontOfSize:15];
        _label.text = @"下拉刷新";
        _label.textColor = [UIColor darkGrayColor];
    }
    return _label;
}
-(void)willMoveToSuperview:(UIView *)newSuperview{
    [super willMoveToSuperview:newSuperview];
    if ([newSuperview isKindOfClass:[UIScrollView class]]) {
        _superscrView =(UIScrollView *) newSuperview;
        [_superscrView addObserver:self forKeyPath:@"contentOffset" options:0 context:nil];
    }
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    NSLog(@"%lf",_superscrView.contentOffset.y);
    if (_superscrView.isDragging) {
        NSLog(@"1");
        CGFloat normalPullingOffset = -60;
        if (_superscrView.contentOffset.y>normalPullingOffset) {
            self.currentStauts =DropDownRefreshViewStautsNormal;
            NSLog(@"Normal");
            
        }else if(_superscrView.contentOffset.y<=normalPullingOffset){
            self.currentStauts=DropDownRefreshViewStautsPulling;
            NSLog(@"Pulling");
        }
    }else{
        NSLog(@"0");
        if (_superscrView.contentOffset.y<=-60) {
            self.currentStauts =DropDownRefreshViewStautsRefreshing;
            NSLog(@"Refreshing");
        }
    }
}
-(void)setCurrentStauts:(DropDownRefreshViewStauts)currentStauts{
    _currentStauts = currentStauts;
    switch (_currentStauts) {
        case DropDownRefreshViewStautsNormal:
            _label.text = @"下拉刷新";
            break;
        case DropDownRefreshViewStautsPulling:
            _label.text = @"释放刷新";
            break;
        case DropDownRefreshViewStautsRefreshing:
            _label.text=@"正在刷新...";
            self.imaView.animationImages  =self.imagArr;
            self.imaView.animationDuration = 0.15*self.imagArr.count;
            [self.imaView startAnimating];
            [UIView animateWithDuration:0.5 animations:^{  self.superscrView.contentInset = UIEdgeInsetsMake(100, self.superscrView.contentInset.left, self.superscrView.contentInset.bottom, self.superscrView.contentInset.right);}];
            NSLog(@"zz%f",self.superscrView.contentInset.top+50);
            
            break;
    }
}
-(NSArray *)imagArr{
    if (_imagArr==nil) {
        NSMutableArray *arr = [NSMutableArray array];
        for (int i =1; i<=5; i++) {
            NSString *str = [NSString stringWithFormat:@"btn_back-%d",i];
            UIImage *imag = [UIImage imageNamed:str];
            [arr addObject:imag];
        }
        _imagArr = arr;
    }
    return _imagArr;
}
@end
